import { GetAllRoomList } from "./service";

const lodash = require('lodash');
// 传入数组 先取出楼层 在使用lodash去重排序 最后循环判断 每条数据和去重后的楼层是否相等 相等想放进对应的数组中
export const useArrsort = (list: getAllRoom[]) => {
    const floors = list.map((item) => item.floor);
    const uniqueFloors = [...new Set(floors)];
    let uniqueArray = lodash.sortBy(uniqueFloors, [Number]);
    const layeredList = uniqueArray.map((floor: string) => ({
        layer: floor,
        list: list.filter((item: { floor: string }) => item.floor === floor),

    }));
    return layeredList
}


export const RoomStatus = (list: getAllRoom[], RoomArr: RoomJIa[]) => {
    let stady = list.map((item: { status: string }) => item.status);
    let counts = lodash.countBy(stady);
    for (let i = 0; i < RoomArr.length; i++) {
        const roomObj = RoomArr[i];
        const roomNum = roomObj.num;
        if (counts.hasOwnProperty(roomNum)) {
            roomObj.RoomNum = counts[roomNum];
        } else {
            roomObj.RoomNum = ""
        }
    }
    return RoomArr
}


export const FlagSwitch = async (
    value: string, // input框 
    selectvalue: string, // 下拉框  
    clickvalue: string, // 房型值
    ChangeBuildNum: string, // 楼栋
    RoomArr: RoomJIa[], // 计算每种房态有多少房间
    RoomFlag: boolean = false  // 是否切换楼栋 切换需要重新计算房态对应的房间
 ) => {
    let arr = []
    let arr2 = []
    let Status: RoomJIa[] = []
    const { data } = await GetAllRoomList({
        name: value,
        Params: selectvalue,
        id: clickvalue,
        b_id: ChangeBuildNum
    });
    const Arrlist = data.list
    if (RoomArr && Arrlist && RoomFlag == true) {
        Status = RoomStatus(Arrlist, RoomArr)
        localStorage.setItem("RoomList", JSON.stringify(Status))
    } else {
        let list = localStorage.getItem("RoomList")
        Status = JSON.parse(list)
    }
    if (data.list) {
        arr = Arrlist
    }
    if (Arrlist) {
        arr2 = useArrsort(Arrlist)
    } 
    // 判断房间是否已过期
    arr.map((item: { checkInEndTime: string | number | Date; status: number; }) => {
        if (isPastflag(item.checkInEndTime)) {
            item.status = 9
        }
    })

    return [arr, arr2, Status]
}

export function calculateDifferenceInDays(givenTime: string | number | Date) {
    // 将给定时间转换为日期对象，并设置时间为当天的00:00:00
    const givenDate = new Date(givenTime);
    givenDate.setHours(0, 0, 0, 0); // 设置时间为当天的开始

    // 获取当前时间的Date对象，并同样设置为当天的00:00:00
    const currentDate = new Date();
    currentDate.setHours(0, 0, 0, 0); // 设置时间为当前天的开始

    // 计算两个日期的差值（以毫秒为单位）
    let differenceInMilliseconds = givenDate - currentDate;

    // 将毫秒差值转换为天数
    let differenceInDays = differenceInMilliseconds / (1000 * 60 * 60 * 24);

    // 如果给定的时间小于当前时间，说明给定时间是在过去，返回“该房间已过期”
    if (differenceInDays < 0) {
        return "该房间已过期";
    }

    // 向上取整计算出的天数
    differenceInDays = Math.ceil(differenceInDays);

    // 返回向上取整后的天数
    return differenceInDays;
}



export function isPastflag(dateString: string | number | Date) {
    if (!dateString) return false
    // 将传入的时间字符串转换为Date对象
    const pastDate = new Date(dateString);
    // 获取当前时间的Date对象
    const now = new Date();

    // 比较两个时间，如果传入的时间小于当前时间，返回true
    return pastDate < now;
}
